package com.ruoyi.aiConversation.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.aiConversation.domain.AiConversation;
import com.ruoyi.aiConversation.service.IAiConversationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * ai对话Controller
 * 
 * @author ruoyi
 * @date 2025-02-11
 */
@RestController
@RequestMapping("/aiConversation/aiConversation")
public class AiConversationController extends BaseController
{
    @Autowired
    private IAiConversationService aiConversationService;

    /**
     * 查询ai对话列表
     */
    @PreAuthorize("@ss.hasPermi('aiConversation:aiConversation:list')")
    @GetMapping("/list")
    public TableDataInfo list(AiConversation aiConversation)
    {
        startPage();
        List<AiConversation> list = aiConversationService.selectAiConversationList(aiConversation);
        return getDataTable(list);
    }

    /**
     * 导出ai对话列表
     */
    @PreAuthorize("@ss.hasPermi('aiConversation:aiConversation:export')")
    @Log(title = "ai对话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiConversation aiConversation)
    {
        List<AiConversation> list = aiConversationService.selectAiConversationList(aiConversation);
        ExcelUtil<AiConversation> util = new ExcelUtil<AiConversation>(AiConversation.class);
        util.exportExcel(response, list, "ai对话数据");
    }



    /**
     * 获取ai对话详细信息
     */
    @PreAuthorize("@ss.hasPermi('aiConversation:aiConversation:query')")
    @GetMapping(value = "/{aiConversationId}")
    public AjaxResult getInfo(@PathVariable("aiConversationId") String aiConversationId)
    {
        return success(aiConversationService.selectAiConversationByAiConversationId(aiConversationId));
    }

    /**
     * 获取ai对话名称
     */
//    @GetMapping("/title")
//    public AjaxResult getTitle(@RequestParam("aiConversationId") String aiConversationId){
////        Long userId = SecurityUtils.getUserId();
////        aiConversation.setUserId(userId);
////        String aiConversationId = aiConversation.getAiConversationId();
//        return success(aiConversationService.selectTitleByAiConversationId(aiConversationId));
//    }
//
//    @GetMapping("/aiConversationId")
//    public AjaxResult getAiConversationId(){
//        Long userId = SecurityUtils.getUserId();
//        Date currentDate = new Date();
//        System.out.println(currentDate);
//        return success(aiConversationService.selectAiConversationId(userId, currentDate));
//    }

    /**
     * 通过userid获取历史对话列表
     */

    @GetMapping("/getListByUserId")
    public TableDataInfo getInfoByUserId()
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Long userId = user.getUserId();
        AiConversation aiConversation = new AiConversation();
        aiConversation.setUserId(userId);
        List<AiConversation> aiConversations = aiConversationService.selectAiConversationByUserId(aiConversation);
        return getDataTable(aiConversations);
    }


    /**
     * 新增ai对话
     */
    @PreAuthorize("@ss.hasPermi('aiConversation:aiConversation:add')")
    @Log(title = "ai对话", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiConversation aiConversation)
    {
        return toAjax(aiConversationService.insertAiConversation(aiConversation));
    }


    /**
     * 修改ai对话
     */
    @PreAuthorize("@ss.hasPermi('aiConversation:aiConversation:edit')")
    @Log(title = "ai对话", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiConversation aiConversation)
    {
        return toAjax(aiConversationService.updateAiConversation(aiConversation));
    }


    /**
     * 删除ai对话
     */
    @PreAuthorize("@ss.hasPermi('aiConversation:aiConversation:remove')")
    @Log(title = "ai对话", businessType = BusinessType.DELETE)
	@DeleteMapping("/{aiConversationIds}")
    public AjaxResult remove(@PathVariable String[] aiConversationIds)
    {
        return toAjax(aiConversationService.deleteAiConversationByAiConversationIds(aiConversationIds));
    }
}
